/**
 * Created by Administrator on 2016/8/16.
 */

var first_btn = $('.first_btn');
var  first_name = $('#first_name');
var first_add_model = $('#first_add');
var first_body = $('#first_body');
var class_two = $('#class_two');
var two_body = $('#two_body');
var current_second;
$(function(){



    init();
    click_init();

    function init(){
        get_firstclass();
    }

    function click_init(){
        first_btn.on('click',function () {
            var  name = first_name.val();

            if (name ==null || name == undefined　|| name == ''){
                alert("添加数据为空")
            }else {
                first_add_model.modal('hide');
                add_first_class(name);
                first_name.val('');
            }

        })
    }


    function add_first_class(name) {
        $.ajax({
            type:'POST',
            url:host+"/news/class",
            data: {classname: name},
            async:false,
            success:function (data) {
                data = $.parseJSON(data);
                alert(data.msg);
                if (data.code == 1){
                    window.location.reload();
                }

            },
            error: function(XMLHttpRequest){
                alert( "请求错误 " + XMLHttpRequest.responseText);
            }
        });
    }

    function get_firstclass() {
        $.ajax({
            type:'GET',
            url:host+"/news/class/parent?parentId=0",

            async:false,
            success:function (data) {
                if(data.code == 1){
                    var temp = '';
                    first_body.empty();
                    $.each(data.list,function (i, item) {
                        temp+=' <tr>'+
                            '<td>'+item.name+'</td>'+
                            '<td>'+
                        ' <button type="button" class="btn btn-info" onclick="edit_first('+item.id+',\''+item.name+'\')">编辑</button>'+
                        ' <button type="button" class="btn btn-info" onclick="to_second('+item.id+')" >二级分类</button>'+
                        ' <button type="button" class="btn btn-info" onclick="delete_first('+item.id+')">删除</button>'+

                        ' </td>'+

                        '</tr>'

                    })
                    first_body.append(temp);
                }
            },
            error: function(XMLHttpRequest){
                alert( "请求错误 " + XMLHttpRequest.responseText);
            }
        });
    }


});



function delete_first(id) {
    if (confirm('确认删除此条目？')){
        $.ajax({
            type:'DELETE',
            url:host+"/news/class?id="+id,
            async:false,
            success:function (data) {
                data = $.parseJSON(data);
                if(data.code == 1){
                    alert("删除成功");
                    window.location.reload();
                }else {
                    alert("删除失败")
                }
            },
            error: function(XMLHttpRequest){
                alert( "请求错误 " + XMLHttpRequest.responseText);
            }
        });
    }
}

function to_second(id) {

    $.ajax({
        type:'GET',
        url:host+"/news/class/parent?parentId="+id,

        async:false,
        success:function (data) {
            if(data.code == 1){
                var temp = '';

                two_body.empty();
                $.each(data.list,function (i, item) {
                    temp+=' <tr>'+
                        '<td>'+item.name+'</td>'+
                        '<td>'+
                        ' <button type="button" class="btn btn-info" onclick="edit_first('+item.id+',\''+item.name+'\')">编辑</button>'+
                        ' <button type="button" class="btn btn-info" onclick="delete_first('+item.id+')">删除</button>'+
                        ' </td>'+
                        '</tr>'

                })
                two_body.append(temp);

                class_two.modal('show');
                current_second = id;
            }else {
                alert("请求出错,请联系开发人员");
            }
        },
        error: function(XMLHttpRequest){
            alert( "请求错误 " + XMLHttpRequest.responseText);
        }
    });
}

function add_class_two() {
    var editname=prompt("新增分类名：",'');
    if (editname!='' || editname==null){
        $.ajax({
            type:'POST',
            url:host+"/news/class",
            data: {classname: editname,parent:current_second},
            async:false,
            success:function (data) {
                data = $.parseJSON(data);
                alert(data.msg);
                if (data.code == 1){
                    window.location.reload();
                }

            },
            error: function(XMLHttpRequest){
                alert( "请求错误 " + XMLHttpRequest.responseText);
            }
        });
    }


}

function edit_first(id,name) {
    var editname=prompt("修改分类名",name);
    if (editname=='' || editname==name ){
    }else {
        $.ajax({
            type:'PUT',
            url:host+"/news/class",
            data: {name: editname,id:id},
            async:false,
            success:function (data) {
                data = $.parseJSON(data);
                if(data.code == 1){
                    alert("修改成功");
                    window.location.reload();
                }else {
                    alert("修改失败")
                }
            },
            error: function(XMLHttpRequest){
                alert( "请求错误 " + XMLHttpRequest.responseText);
            }
        });
    }

}




